<?php

/**
 * MenuItem form base class.
 *
 * @method MenuItem getObject() Returns the current form's model object
 *
 * @package    ##PROJECT_NAME##
 * @subpackage form
 * @author     ##AUTHOR_NAME##
 */
abstract class BaseMenuItemForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                        => new sfWidgetFormInputHidden(),
      'alias_url'                 => new sfWidgetFormInputText(),
      'parent_id'                 => new sfWidgetFormPropelChoice(array('model' => 'MenuItem', 'add_empty' => true)),
      'menu_id'                   => new sfWidgetFormPropelChoice(array('model' => 'Menu', 'add_empty' => true)),
      'content_id'                => new sfWidgetFormPropelChoice(array('model' => 'Content', 'add_empty' => true)),
      'category_id'               => new sfWidgetFormPropelChoice(array('model' => 'Category', 'add_empty' => true)),
      'route'                     => new sfWidgetFormInputText(),
      'weight'                    => new sfWidgetFormInputText(),
      'is_target_blank'           => new sfWidgetFormInputCheckbox(),
      'is_absolute'               => new sfWidgetFormInputCheckbox(),
      'is_separator'              => new sfWidgetFormInputCheckbox(),
      'created_by'                => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => true)),
      'created_at'                => new sfWidgetFormDateTime(),
      'menu_item_permission_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'sfGuardPermission')),
    ));

    $this->setValidators(array(
      'id'                        => new sfValidatorPropelChoice(array('model' => 'MenuItem', 'column' => 'id', 'required' => false)),
      'alias_url'                 => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'parent_id'                 => new sfValidatorPropelChoice(array('model' => 'MenuItem', 'column' => 'id', 'required' => false)),
      'menu_id'                   => new sfValidatorPropelChoice(array('model' => 'Menu', 'column' => 'id', 'required' => false)),
      'content_id'                => new sfValidatorPropelChoice(array('model' => 'Content', 'column' => 'id', 'required' => false)),
      'category_id'               => new sfValidatorPropelChoice(array('model' => 'Category', 'column' => 'id', 'required' => false)),
      'route'                     => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'weight'                    => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647)),
      'is_target_blank'           => new sfValidatorBoolean(),
      'is_absolute'               => new sfValidatorBoolean(),
      'is_separator'              => new sfValidatorBoolean(),
      'created_by'                => new sfValidatorPropelChoice(array('model' => 'sfGuardUser', 'column' => 'id', 'required' => false)),
      'created_at'                => new sfValidatorDateTime(array('required' => false)),
      'menu_item_permission_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'sfGuardPermission', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('menu_item[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'MenuItem';
  }

  public function getI18nModelName()
  {
    return 'MenuItemI18n';
  }

  public function getI18nFormClass()
  {
    return 'MenuItemI18nForm';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['menu_item_permission_list']))
    {
      $values = array();
      foreach ($this->object->getMenuItemPermissions() as $obj)
      {
        $values[] = $obj->getPermissionId();
      }

      $this->setDefault('menu_item_permission_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveMenuItemPermissionList($con);
  }

  public function saveMenuItemPermissionList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['menu_item_permission_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(MenuItemPermissionPeer::MENU_ITEM_ID, $this->object->getPrimaryKey());
    MenuItemPermissionPeer::doDelete($c, $con);

    $values = $this->getValue('menu_item_permission_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new MenuItemPermission();
        $obj->setMenuItemId($this->object->getPrimaryKey());
        $obj->setPermissionId($value);
        $obj->save();
      }
    }
  }

}
